package com.juxfe.bookmanagement.controller;

import com.juxfe.bookmanagement.model.Book;
import com.juxfe.bookmanagement.model.User;
import com.juxfe.bookmanagement.service.BookService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    /**
     * 显示图书列表
     */
    @GetMapping("/list")
    public String list(HttpSession session, Model model, @RequestParam(required = false) String keyword) {
        User user = (User) session.getAttribute("user");
        List<Book> books;

        if (keyword != null && !keyword.trim().isEmpty()) {
            books = bookService.getBooksByCondition(user.getId(), keyword);
            model.addAttribute("keyword", keyword);
        } else {
            books = bookService.getBooksByUserId(user.getId());
        }

        model.addAttribute("books", books);
        return "book/list";
    }

    /**
     * 显示添加图书页面
     */
    @GetMapping("/add")
    public String showAddPage() {
        return "book/add";
    }

    /**
     * 处理添加图书请求
     */
    @PostMapping("/add")
    public String add(@ModelAttribute Book book, HttpSession session) {
        User user = (User) session.getAttribute("user");
        book.setUserId(user.getId());

        bookService.addBook(book);
        return "redirect:/book/list";
    }

    /**
     * 显示编辑图书页面
     */
    @GetMapping("/edit/{id}")
    public String showEditPage(@PathVariable Integer id, Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        Book book = bookService.getBookById(id);

        // 检查图书是否属于当前用户
        if (book != null && book.getUserId().equals(user.getId())) {
            model.addAttribute("book", book);
            return "book/edit";
        } else {
            return "redirect:/book/list";
        }
    }

    /**
     * 处理编辑图书请求
     */
    @PostMapping("/edit")
    public String edit(@ModelAttribute Book book, HttpSession session) {
        User user = (User) session.getAttribute("user");
        book.setUserId(user.getId());

        bookService.updateBook(book);
        return "redirect:/book/list";
    }

    /**
     * 处理删除图书请求
     */
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable Integer id, HttpSession session) {
        User user = (User) session.getAttribute("user");

        bookService.deleteBook(id, user.getId());
        return "redirect:/book/list";
    }
}